import { ref } from 'vue';
import { defineStore } from 'pinia';
import { getVisualization } from '@/api/visualization';

export const useVisualizationStore = defineStore('', () => {
	// 数据是否加载完成
	const isLoading = ref(false);

	// 数据对象
	const data = ref({});
	// 获取数据函数
	const fetchData = async () => {
		try {
			const response = await getVisualization();
			data.value = response;

			isLoading.value = true;
		} catch (e) {
			isLoading.value = false;
		}
	};

	// 定时器标志
	const timerFlag = ref();
	// 开启定时器
	const startRequestData = () => {
		fetchData();
		timerFlag.value = setInterval(fetchData, 10000);
	};
	// 关闭定时器
	const stopRequestData = () => {
		clearInterval(timerFlag.value);
	};

	return { data, isLoading, startRequestData, stopRequestData };
});
